package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddProfessionalActivity extends AppCompatActivity {

    private TextView add_professional_ProfessionalName;
    private Spinner add_professional_CollegeId;
    private Button add_professional_btn;

    private List<String> Collegelist =new ArrayList<String>();
    private List<Integer> CollegeIdlist =new ArrayList<Integer>();
    private int selectCollegeid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_professional);
        initid();
        initcolleglist();
        add_professional_CollegeId.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                selectCollegeid = CollegeIdlist.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

        add_professional_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tijiao();
            }
        });
    }

    private void initid(){
        add_professional_ProfessionalName = (TextView) findViewById(R.id.add_professional_ProfessionalName);
        add_professional_CollegeId = (Spinner) findViewById(R.id.add_professional_CollegeId);
        add_professional_btn = (Button) findViewById(R.id.add_professional_btn);
    }

    private void initcolleglist(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/College/GetAllCollege")
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        String CollegeName = jsonObjects.getString("CollegeName");
                        int CollegeId = jsonObjects.getInt("CollegeId");
                        CollegeIdlist.add(CollegeId);
                        Collegelist.add(CollegeName);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddProfessionalActivity.this,R.layout.classid_item, R.id.text,Collegelist);
                            add_professional_CollegeId.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void tijiao(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    MediaType mediaType = MediaType.parse("application/json");
                    RequestBody requestBody = RequestBody.create(mediaType,"");
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Professional/AddProfessional?token="+Sto.dept.getToken()+"&professionalName="+add_professional_ProfessionalName.getText().toString()+"&CollegeId="+selectCollegeid)
                            .post(requestBody)
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (msg.equals("操作成功")){
                                Toast.makeText(AddProfessionalActivity.this,msg,Toast.LENGTH_SHORT).show();
                                finish();
                            }else {
                                Toast.makeText(AddProfessionalActivity.this,msg,Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}